package com.hsurosy.domain.activity.service.quota;

import com.hsurosy.domain.activity.model.aggregate.CreateQuotaOrderAggregate;
import com.hsurosy.domain.activity.model.entity.*;
import com.hsurosy.domain.activity.model.valobj.ActivitySkuStockKeyVO;
import com.hsurosy.domain.activity.model.valobj.OrderStateVO;
import com.hsurosy.domain.activity.repository.IActivityRepository;
import com.hsurosy.domain.activity.service.IRaffleActivitySkuStockService;
import com.hsurosy.domain.activity.service.quota.policy.ITradePolicy;
import com.hsurosy.domain.activity.service.quota.rule.factory.DefaultActivityChainFactory;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @Author Hsu琛君珩
 * @Date 2024-11-04 17:58
 * @Description 抽奖活动服务
 * @Version: v1.0.0
 */
@Service
public class RaffleActivityAccountQuotaService extends AbstractRaffleActivityAccountQuota implements IRaffleActivitySkuStockService {

    public RaffleActivityAccountQuotaService(DefaultActivityChainFactory defaultActivityChainFactory, IActivityRepository activityRepository, Map<String, ITradePolicy> tradePolicyGroup) {
        super(defaultActivityChainFactory, activityRepository, tradePolicyGroup);
    }

    /**
     * 构建订单聚合对象
     *
     * @param skuRechargeEntity SKU 充值实体
     * @param activitySkuEntity 活动 SKU 信息
     * @param activityEntity 活动基本信息
     * @param activityCountEntity 活动次数信息
     * @return 创建的订单聚合对象
     */
    @Override
    protected CreateQuotaOrderAggregate buildOrderAggregate(SkuRechargeEntity skuRechargeEntity, ActivitySkuEntity activitySkuEntity, ActivityEntity activityEntity, ActivityCountEntity activityCountEntity) {
        // 构建订单实体对象，包含用户信息、活动信息和订单详情
        ActivityOrderEntity activityOrderEntity = new ActivityOrderEntity();
        activityOrderEntity.setUserId(skuRechargeEntity.getUserId());
        activityOrderEntity.setSku(skuRechargeEntity.getSku());
        activityOrderEntity.setActivityId(activityEntity.getActivityId());
        activityOrderEntity.setActivityName(activityEntity.getActivityName());
        activityOrderEntity.setStrategyId(activityEntity.getStrategyId());
        // 生成订单ID（生产环境中通常使用雪花算法或UUID生成服务）
        activityOrderEntity.setOrderId(RandomStringUtils.randomNumeric(12));
        activityOrderEntity.setOrderTime(new Date());
        activityOrderEntity.setTotalCount(activityCountEntity.getTotalCount());
        activityOrderEntity.setDayCount(activityCountEntity.getDayCount());
        activityOrderEntity.setMonthCount(activityCountEntity.getMonthCount());
        activityOrderEntity.setPayAmount(activitySkuEntity.getProductAmount());
        activityOrderEntity.setOutBusinessNo(skuRechargeEntity.getOutBusinessNo());

        // 构建并返回聚合对象，包含订单和用户参与次数信息
        return CreateQuotaOrderAggregate.builder()
                .userId(skuRechargeEntity.getUserId())
                .activityId(activitySkuEntity.getActivityId())
                .totalCount(activityCountEntity.getTotalCount())
                .dayCount(activityCountEntity.getDayCount())
                .monthCount(activityCountEntity.getMonthCount())
                .activityOrderEntity(activityOrderEntity)
                .build();
    }

    /**
     * 从库存扣减队列中获取下一个待处理的库存键值对象
     *
     * @return ActivitySkuStockKeyVO 包含SKU和活动ID的键值对象
     * @throws InterruptedException 如果获取操作被中断
     */
    @Override
    public ActivitySkuStockKeyVO takeQueueValue() throws InterruptedException {
        return activityRepository.takeQueueValue();
    }

    /**
     * 清空库存扣减队列
     */
    @Override
    public void clearQueueValue() {
        activityRepository.clearQueueValue();
    }

    /**
     * 更新指定 SKU 的库存信息
     *
     * @param sku 指定的商品SKU，用于更新其最新的库存状态。
     */
    @Override
    public void updateActivitySkuStock(Long sku) {
        activityRepository.updateActivitySkuStock(sku);
    }

    /**
     * 清除指定 SKU 的缓存库存信息
     *
     * @param sku 指定的商品SKU，用于清除其缓存数据。
     */
    @Override
    public void clearActivitySkuStock(Long sku) {
        activityRepository.clearActivitySkuStock(sku);
    }

    /**
     * 更新订单信息
     *
     * @param deliveryOrderEntity 包含订单更新信息的实体对象
     */
    @Override
    public void updateOrder(DeliveryOrderEntity deliveryOrderEntity) {
        activityRepository.updateOrder(deliveryOrderEntity);
    }

    /**
     * 查询用户当天在指定活动中的参与次数
     *
     * @param activityId 活动ID，用于标识具体的抽奖活动
     * @param userId 用户ID，用于查询该用户的参与记录
     * @return 返回用户当日的参与次数
     */
    @Override
    public Integer queryRaffleActivityAccountDayPartakeCount(Long activityId, String userId) {
        return activityRepository.queryRaffleActivityAccountDayPartakeCount(activityId, userId);
    }

    /**
     * 根据活动ID和用户ID查询用户的活动账户信息
     *
     * @param activityId 活动ID
     * @param userId 用户ID
     * @return 活动账户实体对象，包含总额度、剩余额度、月额度、日额度等信息
     */
    @Override
    public ActivityAccountEntity queryActivityAccountEntity(Long activityId, String userId) {
        return activityRepository.queryActivityAccountEntity(activityId, userId);
    }

    /**
     * 查询用户在指定活动中的总参与次数
     *
     * @param activityId 活动ID
     * @param userId 用户ID
     * @return 用户在该活动中的总参与次数
     */
    @Override
    public Integer queryRaffleActivityAccountPartakeCount(Long activityId, String userId) {
        return activityRepository.queryRaffleActivityAccountPartakeCount(activityId, userId);
    }

}
